package leetcode.code1981;

import java.util.HashSet;
import java.util.Set;

import leetcode.IDebug;
import leetcode.helper.H;

/**
 * 小数据量
 * 
 * 执行用时：221 ms, 在所有 Java 提交中击败了81.56%的用户
 * 
 * 内存消耗：41.4 MB, 在所有 Java 提交中击败了79.43%的用户
 * 
 * 通过测试用例：81 / 81
 * 
 * @date 2022年5月28日
 * @description
 */
public class Solution implements IDebug {

	public int minimizeTheDifference(int[][] mat, int target) {
		int m = mat.length, n = mat[0].length;
		boolean[] set = new boolean[4901];
		for (int i = 0; i < n; i++) {
			set[mat[0][i]] = true;
		}
		for (int i = 1; i < m; i++) {
			boolean[] cur = new boolean[4901];
			for (int k = 0; k < 4901; k++) {
				if (set[k]) {
					for (int j = 0; j < n; j++) {
						int num = mat[i][j];
						if (num + k < 5701) //
							cur[num + k] = true;
					}
				}

			}
			set = cur;
		}
		int ans = Integer.MAX_VALUE;
		for (int k = 0; k < 4901; k++) {
			if (set[k]) {
				ans = Math.min(ans, Math.abs(k - target));
			}
		}
		return ans;
	}

	@Override
	public void debug4() {
		H.compare(1510, this.minimizeTheDifference(H.str2array1(
				"[[65],[45],[45],[69],[55],[60],[29],[25],[16],[5],[62],[16],[29],[19],[34],[2],[24],[32],[66],[62],[60],[46],[42],[37],[51],[4],[41],[4],[66],[20],[9],[4],[66],[6],[56],[10],[51],[44],[7],[8],[5],[44],[28],[7],[10],[7],[24],[62],[19],[14],[45],[68],[9],[14],[51],[28],[8],[57],[59],[6],[54],[8],[19],[16],[63],[45],[33],[15],[33],[67]]"),
				800));

	}

	@Override
	public void debug3() {
		// TODO Auto-generated method stub

	}

	@Override
	public void debug2() {
		// TODO Auto-generated method stub

	}

	@Override
	public void debug1() {
		// TODO Auto-generated method stub

	}

	public static void main(String[] args) {
		Solution so = new Solution();
		so.debug1();
		so.debug2();
		so.debug3();
		so.debug4();

	}

}
